﻿using DrCarDrive.DBService;
using DrCarDrive.Util;
using DrCarDrive.Wpf.WpfWindow;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DrCarDrive.Wpf.WpfPage
{
    /// <summary>
    /// Interaction logic for FuelTopUp.xaml
    /// </summary>
    public partial class FuelTopUp : Page
    {
        private static FuelTopUp instance;
        private FuelTopupService fuelTopupService;
        public FuelTopUp()
        {
            InitializeComponent();
            tbl_fuel ftu = new tbl_fuel();
            this.DataContext = ftu;

        }

        public static object Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new FuelTopUp();
                }

                return instance;
            }
        }

        private void btnFuelCancel_Click(object sender, RoutedEventArgs e)
        {
            resetFuelTopupWindow();
        }

        private void resetFuelTopupWindow()
        {
            txtFuelAmount.Text = "";
            txtFuelOdometer.Text = "";
            txtFuelVolume.Text = "";
            txtFuelRemark.Text = "";
            dtpFuelDateTime.Text = "";
        }

        private void btnFuelSave_Click(object sender, RoutedEventArgs e)
        {
           
            if (validate() == true)
            {
                tbl_fuel fuelTopup = new tbl_fuel();
                fuelTopup.ID = GUIDGenerator.fuelKey();
                DateTime topupDateTime = DateTime.ParseExact(dtpFuelDateTime.Text, "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture);
                fuelTopup.ADD_TIME = topupDateTime;
                
                double odometer;
                Double.TryParse(txtFuelOdometer.Text,out odometer);
                fuelTopup.ODOMETER = odometer;

                double volume;
                Double.TryParse(txtFuelVolume.Text,out volume);
                fuelTopup.VOLUME = volume;

                decimal amount;
                Decimal.TryParse(txtFuelAmount.Text,out amount);
                fuelTopup.AMOUNT = amount;

                fuelTopup.FKY_DRIVE = SessionData.drive.ID;
                fuelTopupService = new FuelTopupService();
                //MessageBox.Show();
                int status = fuelTopupService.save(fuelTopup);
                if (status > 0)
                {
                    MSGWindow success = new MSGWindow(Messages.TTL_FUEL_TOPUP, Messages.MSG_FUEL_TOPUP_ADD);
                    success.ShowDialog();
                    MainWindow.Instance.ContentFrame.Content = DashboardPage.Instance;
                    destroy();
                }
            }
            else
            {
                MSGWindow errorWindow = new MSGWindow(Messages.TTL_FILL_REQUIRED, Messages.MSG_FILL_REQUIRED_INFORMATION);
                errorWindow.ShowDialog();
            }
        }

        private Boolean validate()
        {
            if (txtFuelOdometer.Text == "" || dtpFuelDateTime.Value == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public static void destroy()
        {
            instance = null;
        }

        private void txtFuelOdometer_KeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = !IsNumberKey(e.Key) && !IsActionKey(e.Key);
        }
        private bool IsNumberKey(Key inKey)
        {
            if (inKey < Key.D0 || inKey > Key.D9)
            {
                if (inKey < Key.NumPad0 || inKey > Key.NumPad9)
                {
                    return false;
                }
            }
            return true;
        }

        private bool IsActionKey(Key inKey)
        {
            return inKey == Key.Delete || inKey == Key.Back || inKey == Key.Tab || inKey == Key.Return || Keyboard.Modifiers.HasFlag(ModifierKeys.Alt);
        }

    }
}
